package com.dao;

import com.bean.LoginBean;
import com.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDAO {
	public static String loginGateway(LoginBean lgb) {
		System.out.println("Hi User ! --> LoginController.doPost()-->LoginDAO.loginGateway()");
		Connection connObj = DBConnection.Connect(); // Establish Connection ;
		if (connObj == null) {
			System.out
					.println("Sorry. We could not connect to the Server. \n Try again later ! ");

		} else {
			System.out.println("Server Connected.... ! ");
			System.out.println("Connection Object Details :");
			System.out.println(connObj);
		}

		try {
			
			connObj.commit();
			 PreparedStatement pstmt = connObj
			 .prepareStatement("SELECT * FROM Login where USERNAME = ? AND PASSWORD  = ?");
			 pstmt.setString(1,lgb.getUserID());
			 pstmt.setString(2,lgb.getPassword());
			 ResultSet rs = pstmt.executeQuery();
			
//			PreparedStatement pstmt = connObj.prepareStatement("CREATE TABLE IF NOT EXISTS login(username varchar(20), password varchar (50), role varchar(25))");
//			int rs = pstmt.executeUpdate();
//			pstmt = connObj.prepareStatement("INSERT into LOGIN VALUES('Chitra','pass','Admin')");
//			rs = pstmt.executeUpdate();
			
			if(rs.next())
			{
				System.out.println("Login Success !");
			    return rs.getString(3);
			}
			else{
				System.out.println("Invalid Login");
				return null;
			}
			

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			

		}
		return null;

	}


	public static boolean changePassword(LoginBean lgb) {
		Connection connObj = DBConnection.Connect(); // Establish Connection ;
		if (connObj == null) {
			System.out
					.println("Sorry. We could not connect to the Server. \n Try again later ! ");

		} else {
			System.out.println("Server Connected.... ! ");
			System.out.println("Connection Object Details :");
			System.out.println(connObj);
		}

		try 
		{

			connObj.commit();
			PreparedStatement pstmt = connObj
					.prepareStatement("SELECT * FROM Login where USERID = ? AND PASSWORD  = ?");
			pstmt.setString(1,lgb.getUserID() );
			pstmt.setString(2,lgb.getPassword());
			
			ResultSet rs = pstmt.executeQuery();

			if (rs.next())
			{
				System.out.println("Found User !");
				System.out.println("UserName : " + rs.getString(1));
				System.out.println("Pasword  : " + rs.getString(2));

				PreparedStatement updatePwd = connObj
						.prepareStatement("update Login set password = ? where userid = ?");
				updatePwd.setString(1,lgb.getNewPassword());
				updatePwd.setString(2,lgb.getUserID());

				
				updatePwd.executeUpdate();
				connObj.commit();
				return true;
			} 
			else 
			{

				System.out.println("Invalid Details");
				return false;
			}

		} 
			catch (SQLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
		return false;

	}
}
